<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true , isEdit = false"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="redact(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteFn(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pagination.page"
                :page-sizes="[1, 2, 3, 5]"
                :page-size="pagination.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        title="编辑弹层"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      activeName: 'first',
      roles: [],
      pagination: {
        page: 1, // 当前页数
        pagesize: 5 // 每页多少条
      },
      showDialog: false,
      total: 1, // 一共多少条
      roleForm: {
        name: '',
        description: ''
      },
      isEdit: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pagination)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    async doEdit() {
      try {
        const res = await updateRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },

    async doAdd() {
      try {
        this.pagination.page = Math.ceil(++this.total / this.pagination.pagesize)
        const res = await addRole(this.roleForm)
        console.log(res)
        this.$message.success('添加成功')
        console.log(1250)
        this.loadRoles()
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pagination.pagesize = val
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pagination.page = val
      this.loadRoles()
    },
    async loadDeleteRole(id) {
      try {
        const res = await deleteRole(id)
        console.log(res)
        // 1. 提示
        console.log('-------------------')
        this.$message.success('删除成功')
        if (this.roles.length === 1) {
          this.pagination.page--
          if (this.pagination.page <= 0) {
            this.pagination.page = 1
          }
        }
        // 2. 重新请求
        this.loadRoles()
      } catch (err) {
        console.dir(err)
        console.log('**************************')
        this.$message.error('删除失败')
      }
    },
    deleteFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadDeleteRole(id)
        this.loadRoles()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 用户点击了保存
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          // 做具体的添加动作
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    redact(row) {
      console.log(row)
      this.isEdit = true
      this.showDialog = true
      this.roleForm = { ...row }
    }// 编辑
  }
}

</script>
